{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pandas的Series类似于numpy中的array，只不过在array加了一个index\n",
    "常见的创建模式如下：\n",
    "import pandas as pd\n",
    "series=pd.Series(data,index=idx)\n",
    "\n",
    "数据data可以是：\n",
    "ndarray\n",
    "python dictionary\n",
    "scalar value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#np.random.seed(100)\n",
    "series1=pd.Series(np.random.rand(7))\n",
    "series1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "currDict={'US' : 'dollar', 'UK' : 'pound', \n",
    "         'Germany': 'euro', 'Mexico':'peso',\n",
    "         'Nigeria':'naira',\n",
    "        'China':'yuan', 'Japan':'yen'}\n",
    "series_curr=pd.Series(currDict,index=['China','US','UK','Germany','Mecico','Nigeria','Japan'],name='current')\n",
    "#index设置索引的顺序，可以自定义顺序，name为series的名称\n",
    "series_curr\n",
    "print series_curr['China']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 a         b         c\n",
      "jiangxi   0.223648  0.854896  0.283974\n",
      "beijing   0.683538  0.805152  0.942835\n",
      "shanghai  0.547935  0.158467  0.015563\n",
      "tianjing  0.880128  0.801255  0.851997\n",
      "============\n",
      "a    0.656481\n",
      "b    0.696429\n",
      "c    0.927272\n",
      "dtype: float64\n",
      "============\n",
      "jiangxi     0.631248\n",
      "beijing     0.259297\n",
      "shanghai    0.532372\n",
      "tianjing    0.078874\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "df1 = pd.DataFrame(np.random.rand(4,3),index = ['jiangxi','beijing','shanghai','tianjing'],columns = list('abc'))\n",
    "f = lambda x : x.max() - x.min()\n",
    "print df1\n",
    "#注意，默认axis=0，说明对列进行操作，axis=1对行进行操作\n",
    "print \"============\"\n",
    "print df1.apply(f)\n",
    "print \"============\"\n",
    "print df1.apply(f,axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
